using System.Collections.Generic;
using System.Linq;
using Microsoft.Office.Core;
using Office.Utility.Extensions;
using MSOffice = Microsoft.Office.Core;

namespace Office.Utility.Toolbar
{
    public abstract class ToolbarControl
	{
	    readonly List<CommandBarControl> _listControls = new List<CommandBarControl>();
	    public string Id { get; set; }
	    public string Caption { get; set; }
	    public string ToolTip { get; set; }
	    public int? Position { get; set; }

	    protected ToolbarControl(string id)
		{
			Id = id;
			Caption = id;
		}

	    protected ToolbarControl(string id, string caption)
		{
			Id = id;
			Caption = caption;
		}

	    protected ToolbarControl(string id, string caption, string tooltip)
			: this(id, caption)
		{
			ToolTip = tooltip;
		}

		protected CommandBarControl FindControl(MSOffice.CommandBar bar)
		{
		    return bar.Controls.ComLinq<CommandBarControl>().FirstOrDefault(ctl => ctl.Parameter == Id);
		}

	    protected TControl FindControl<TControl>(MSOffice.CommandBar bar) where TControl : class
		{
			var ctl = FindControl(bar);

			if (ctl != null)
			{
			    if (!(ctl is TControl))
				{
					ctl.Delete(false);
					return null;
				}
			    return (TControl)ctl;
			}

		    return null;

		}


		protected void AddControl(CommandBarControl control)
		{
			_listControls.Add(control);
		}

		protected void RemoveControl(CommandBarControl control)
		{
			_listControls.Remove(control);
		}

		internal abstract void Attach(MSOffice.CommandBar bar, WindowAdapter window);


	}
}
